﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;

public partial class administrator_usercontrol_LSPmanager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillGrid();
            lbThongBao.Text = "";
        }
    }

    private void fillGrid()
    {
        List<LoaiSanPhamBUS> loaisanphams = LoaiSanPhamBUS.GetAll();
        GridView1.AutoGenerateColumns = false;
        GridView1.DataSource = loaisanphams;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        fillGrid();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string Ma = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMa")).Text;
        string Ten = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTen")).Text;
        LoaiSanPhamBUS lsp = LoaiSanPhamBUS.GetByID(ID);
        lsp.MaLoai = Ma;
        lsp.TenLoai = Ten;
        lsp.Update();
        GridView1.EditIndex = -1;
        Response.Redirect("loaisanpham.aspx");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Response.Redirect("loaisanpham.aspx");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            string ten = ((TextBox)GridView1.FooterRow.FindControl("txtTeninsert")).Text;
            string Ma = ((TextBox)GridView1.FooterRow.FindControl("txtMainsert")).Text;
            if (Ma == null || Ma.Trim() == "")
            {
                lbThongBao.Text = "Mã Loại không thể bỏ trống !!!";
            }
            else
            {
                LoaiSanPhamBUS lsp = new LoaiSanPhamBUS()
                {
                    MaLoai = Ma,
                    TenLoai = ten,
                };
                try
                {
                    lsp.Add();
                    lbThongBao.Text = "";
                }
                catch { lbThongBao.Text = "trùng mã loại !!!"; }
                fillGrid();
            }
        }
        if (e.CommandName == "Delete")
        {
            int ID = Convert.ToInt32(e.CommandArgument);
            LoaiSanPhamBUS lsp = LoaiSanPhamBUS.GetByID(ID);
            try
            {
                lsp.Delete();
                Response.Redirect("loaisanpham.aspx");
            }
            catch
            {
                lbThongBao.Text = "Không Thể Xóa Dữ Liệu !!!";
            }

        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lkbXoa");

            l.Attributes.Add("onclick", "javascript:return " +
                 "confirm('bạn có muốn xóa Loại sản phẩm " +
                 DataBinder.Eval(e.Row.DataItem, "MaLoai") + " ?')");

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}
